using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Entidades;

namespace Jetaime.Origen
{
    class OrigenController
    {
        #region PROPIEDADES
        private Jetaime.OrigenView oView;
        private OrigenE origen;
        private OrigenModel oModel;
        private String ABM = String.Empty;
        #endregion        
        
        #region CONSTRUCTORES
        public OrigenController(String ABM, OrigenE origen): this(ABM)
        {   
            this.origen = origen;                 
            oView.txtDescripcion.Text = this.origen.Descripcion;
        }
        
        public OrigenController(String ABM)
        {
            this.oView = new OrigenView();
            this.oModel = new OrigenModel();
            this.ABM = ABM;
            this.InitEvent();    
        }
        #endregion
        
        #region METODOS
        private void InitEvent()
        {
            oView.Load += new EventHandler(oView_Load);
            oView.btnAceptar.Click += new EventHandler(btnAceptar_Click);
            oView.btnCancelar.Click += new EventHandler(btnCancelar_Click);
            oView.txtDescripcion.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtDescripcion_KeyPress);
        }

        void oView_Load(object sender, EventArgs e)
        {
            oView.txtDescripcion.Focus();
        }

        void txtDescripcion_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnAceptar_Click(null, null);
            }
        }

        void btnCancelar_Click(object sender, EventArgs e)
        {
            oView.Close();
        }

        void btnAceptar_Click(object sender, EventArgs e)
        {   
            if(this.ABM == "A")
            {
                OrigenE origen = new OrigenE();
                origen.Descripcion = this.oView.txtDescripcion.Text;
                this.oModel.Guardar("A", origen);                 
            }
            else if(this.ABM == "M")
            {
                this.origen.Descripcion = oView.txtDescripcion.Text;
                oModel.Guardar("M", this.origen);
            }
            this.oView.Close();
        }
        
        public void Show()
        {
            oView.ShowDialog();
        }
        
        private void CargarTextBox(OrigenE origen)
        {
            oView.txtDescripcion.Text = origen.Descripcion;
        }
        #endregion        
    }
}
